package com.winchain.chainbill.bill.biz.discount.rx;

import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.MessageSender;
import com.shell.framework.utils.SysDateUtil;
import com.shell.governor.modules.message.keyword.SysDctRevRejectKeyword;
import com.winchain.chainbill.bill.base.po.CbDct;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.BillTranEvent;
import com.winchain.chainbill.bill.biz.discount.service.BillDctEventService;
import com.winchain.chainbill.bill.biz.send.rx.BatchBillEvent;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillDctRevRejectMsgSubscriber extends OnErrorNotImplementedSubscriber<BatchBillEvent<String, CbDct>> {
	@Autowired
	private BillDctEventService billDctEventService;

	public void onApplicationEvent(BatchBillEvent<String, CbDct> t) {
		List<String> records = t.getRecs();
		for (String record : records) {
			BillTranEvent<CbDct> event = this.billDctEventService.getBillTranEvent(record, false);
			Bill<CbDct> prntBill = event.getBill();
			List<Bill<CbDct>> children = (List<Bill<CbDct>>) event.getChildren();
			CbDct cbDct = null;
			if (children.size() == 0) {
				cbDct = (CbDct) prntBill.getSource();
			} else {
				for (Bill<CbDct> bill : children) {
					if (((CbDct) bill.getSource()).getDctId() != null) {
						cbDct = (CbDct) bill.getSource();
					}
				}
			}
			String template = "MT01613";
			SysDctRevRejectKeyword keyWord = new SysDctRevRejectKeyword();
			keyWord.setDctAmt(String.valueOf(cbDct.getDctAmt()));

			String dctDate = SysDateUtil.parseDate(cbDct.getDctDate(), "yyyyMMdd", "yyyy-MM-dd");
			keyWord.setDctDate(dctDate);
			keyWord.setContactorName(CommonDataUtil.getUserName());
			keyWord.setRejectReason((String) t.getValue("remark"));

			String senderId = CommonDataUtil.getUserId();

			MessageSender.sendMessageByTmpl(senderId, template, keyWord, cbDct.getSellerId());
		}
	}
}
